﻿#region itdc

/************************************************************************
*Copyright(C) 2015 by qsgy
 *All rights reserved. 
 *FileName:     $safeitemname$
 *Author:       oyh
 *Version:      
 *UnityVersion：
 *Date:   $time$      
 *Description:    
 *History: 
************************************************************************/
#endregion
using UnityEngine;

public class Rod : MonoBehaviour {
    #region 1
    public Vector3 tagert;// y and z
    public float speed = 1f;
    public Vector3 max;
    public Vector3 min;
    private void Awake()
    {
        tagert = transform.localPosition;
    }

    // Update is called once per frame
    void Update()
    {
        if (tagert.x>max.x)
        {
            tagert.x = max.x;
           // Application.ExternalCall("ExceptionOP",name,"x","max");
            ByteBuffer bf = ByteBuffer.Allocate(15);
            bf.WriteShort(13);
            bf.WriteUTF("Exce");
            bf.WriteUTF("x max");
            SocketFace.instance.SendBuf(bf);
        }
        if (tagert.y > max.y)
        {
            tagert.y = max.y;
            // Application.ExternalCall("ExceptionOP", name, "y", "max");
            ByteBuffer bf = ByteBuffer.Allocate(15);
            bf.WriteShort(13);
            bf.WriteUTF("Exce");
            bf.WriteUTF("y max");
            SocketFace.instance.SendBuf(bf);
        }
        if (tagert.z > max.z)
        {
            tagert.z = max.z;
            // Application.ExternalCall("ExceptionOP", name, "z", "max");
            ByteBuffer bf = ByteBuffer.Allocate(15);
            bf.WriteShort(13);
            bf.WriteUTF("Exce");
            bf.WriteUTF("z max");
            SocketFace.instance.SendBuf(bf);
        }
        if (tagert.z < min.z)
        {
            tagert.z = min.z;
            //Application.ExternalCall("ExceptionOP", name, "z", "min");
            ByteBuffer bf = ByteBuffer.Allocate(15);
            bf.WriteShort(13);
            bf.WriteUTF("Exce");
            bf.WriteUTF("z min");
            SocketFace.instance.SendBuf(bf);
        }
        if (tagert.x < min.x)
        {
            tagert.x = min.x;
            // Application.ExternalCall("ExceptionOP", name,"x", "min");
            ByteBuffer bf = ByteBuffer.Allocate(15);
            bf.WriteShort(13);
            bf.WriteUTF("Exce");
            bf.WriteUTF("x min");
            SocketFace.instance.SendBuf(bf);
        }
        if (tagert.y < min.y)
        {
            tagert.y = min.y;
            //Application.ExternalCall("ExceptionOP", name, "y", "min");
            ByteBuffer bf = ByteBuffer.Allocate(15);
            bf.WriteShort(13);
            bf.WriteUTF("Exce");
            bf.WriteUTF("y min");
            SocketFace.instance.SendBuf(bf);
        }


        if ((tagert - transform.localPosition).magnitude >= 0.01f)
        {
            //  transform.Translate((tagert - transform.position).normalized * speed*Time.deltaTime);
            transform.localPosition = transform.localPosition + (tagert - transform.localPosition).normalized * speed * Time.deltaTime;
        }
    }

    #endregion

    //输出位置坐标 格式[x,y,z]
    public void GetPos()
    {
      //  Debug.Log("[" + transform.position.x + "," + transform.position.y + "," + transform.position.z + "]");
    }

    //设置移动速度
    public void SetSpeed(float sp)
    {
        speed = sp;
    }

    //设置目标位置。初始坐标为0,0,0
    public void SetTagert(float x,float y,float z)
    {
        tagert = new Vector3(x, y, z);
    }

    public void SetX(float x)
    {
        tagert = new Vector3(x, tagert.y, tagert.z);

    }


    public void SetY(float y)
    {
        tagert = new Vector3(tagert.x, y, tagert.z);
    }

    public void SetZ(float z)
    {
        tagert = new Vector3(tagert.x, tagert.y, z);
    }
}
